using System;
using System.Text;
using System.Resources;
using Microsoft.SharePoint.WebPartPages;

using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using ThoughtWorks.CruiseControl.Remote;


namespace CruiseControlStatus
{
    [XmlRoot(Namespace = "CruiseControlStatus")]
    public class ccStatus : WebPart  
    {
        private IRemotingService _RemotingService=null;        

        private ICruiseManager _CruiseManager=null;

        public ICruiseManager CruiseManager
        {
            get 
            { 
                if (_CruiseManager == null)
                    _CruiseManager = (ICruiseManager)RemotingService.Connect(typeof(ICruiseManager), ServerUri);
                return _CruiseManager; 
            }
            
        }

        private ProjectStatus[] _ProjectStatusList=null;

        public ProjectStatus[] ProjectStatusList
        {
            get 
            {
                if (_ProjectStatusList == null)
                {
                    _ProjectStatusList = CruiseManager.GetProjectStatus();
                }
                return _ProjectStatusList; 
            }
            
        }


        public IRemotingService RemotingService
        {
            get 
            {
                if (_RemotingService == null)
                    _RemotingService = new RemotingServiceAdapter();

                return _RemotingService; 
            }
            
        }

        public string ServerUri
        {
            get
            {
                return "tcp://"+ServerName+":"+Port+"/CruiseManager.rem";
            }
        }


        private string _ServerName = "localhost";

        [Browsable(true), Category("Cruise Control Settings"),        
        WebPartStorage(Storage.Shared),
        FriendlyName("Server Name")]
        public string ServerName
        {
            get { return _ServerName; }
            set { _ServerName = value; }
        }

        private string _Port="21234";
        [Browsable(true), Category("Cruise Control Settings"),
            WebPartStorage(Storage.Shared),
            FriendlyName("Port")]
        public string Port
        {
            get { return _Port; }
            set { _Port = value; }
        }

        private void GetProjectStatus(StringBuilder sb, ProjectStatus status)
        {
            Template TemplateRender = null;
            if (status.BuildStatus == IntegrationStatus.Success)
                TemplateRender = new Template("SucessTemplate.htm");
            else
                TemplateRender = new Template("FailureTemplate.htm");

            TemplateRender.SetValue("{$ProjectName}", status.Name);
            if (status.Activity.IsBuilding())
                TemplateRender.SetValue("{$BuildStatus}", "Building");

            if (status.Activity.IsPending())
                TemplateRender.SetValue("{$BuildStatus}", "Pending");

            if (status.Activity.IsSleeping())
                TemplateRender.SetValue("{$BuildStatus}", "Sleeping");

            TemplateRender.SetValue("{$LastBuildStatus}", status.BuildStatus.ToString());

            TemplateRender.SetValue("{$LastBuildDate}", status.LastBuildDate.ToShortDateString() + " " + status.LastBuildDate.ToShortTimeString());
            TemplateRender.SetValue("{$CurrentMessage}", status.CurrentMessage);
            TemplateRender.SetValue("weburl.htm", status.WebURL);
            TemplateRender.SetValue("{$LastBuildLabel}", status.LastBuildLabel);
            TemplateRender.SetValue("{$LastSuccessfulBuildLabel}", status.LastSuccessfulBuildLabel);
            
           

            sb.Append(TemplateRender.ToString());
          
        }

         

        public Control CreateCruiseButton(string projName)
        {
            return new ProjectHtmlButton(CruiseManager,  projName);                        
        }


        private TextLabel CreateText(string txt)
        {
            return new TextLabel(txt);            
        }

        protected override void CreateChildControls()
        {
            try
            {

                for (int i = 0; i < ProjectStatusList.Length; i++)
                {
                    ProjectStatus pstatus = ProjectStatusList[i];
                    StringBuilder sb = new StringBuilder();
                    GetProjectStatus(sb, pstatus);                    
                    Controls.Add(CreateText(sb.ToString()));
                    Controls.Add(CreateText("<div align=\"right\" style=\"padding-right:5px\" >"));
                    Controls.Add(CreateCruiseButton(pstatus.Name));
                    Controls.Add(CreateText("</div>"));
                    Controls.Add(CreateText("<hr>"));
                }               

            }
            catch (Exception e)
            {
                Controls.Add(CreateText(e.Message));                  
            }

        }

        protected override void Render(HtmlTextWriter writer)
        {
            RenderChildren(writer);            
        }
    }

  
   

}
